// pages/home/messageList/messageList.js
import * as API from '../../../api/homeApi';
import * as util from '../../../utils/util';
// import * as redDot from '../../utils/redDot';

// let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholder: '输入客户名称/客户编码搜索',
    searchKey: '',
    pageNo: 1,
    pageSize: 20,
    hasRequest: false,
    networkError: false,
    messageType: 'business',
    dataSource: [],
    isLogin: true,
    unReadNum: 0,
    showPlace: false,
    orderId: '', //当前操作的订单ID
    messageId: '', //当前操作的消息ID
    total: 0
  },

  changeMessageType: function (options) {
    let messageType = options.currentTarget.dataset.name;
    this.setData({
      messageType: messageType
    })
  },

  clear: function () {
    // 清空数据重新加载
    this.setData({
      dataSource: [],
      pageNo: 1,
      pageSize: 20,
      hasMore: false
    });
  },

  closePlace: function () {
    this.setData({
      showPlace: false
    })
  },

  longTouch: function (e) {
    this.setData({
      showPlace: true,
      messageId: e.target.dataset.messageId,
      orderId: e.target.dataset.orderId
    })
  },

  //查看详情
  gotoOrderDetail: function (e) {
    this.setData({
      showPlace: false
    })
    const orderId = e.currentTarget.dataset.orderId;
    const messageId = e.currentTarget.dataset.messageId;
    const nodeId = e.currentTarget.dataset.nodeId;
    const type = e.currentTarget.dataset.type;
    if (type == '3') {
      util.preventMoreClick(this, () => {
        wx.navigateTo({
          url: '../../order/followers/followers?orderId=' + orderId
        })
      })
    } else {
      util.preventMoreClick(this, () => {
        wx.navigateTo({
          url: '../../order/orderProcess/orderProcess?orderId=' + orderId + "&messageId=" + messageId + "&nodeId=" + nodeId
        })
      })
    }
  },

  // 标记为已读单个
  markAsReadOne: function (e) {
    const messageId = e.currentTarget.dataset.messageId;
    let postbody = {
      messageId: messageId
    }
    API.markAsReadOne(postbody).then(res => {
      this.setData({
        showPlace: false
      })
      this.onShow();
      util.toast('已设为已读');
    }, err => {
      util.toast(err || '标记失败');
    })
  },

  //删除消息
  removeMsg: function (e) {
    const messageId = e.currentTarget.dataset.messageId;
    let postBody = {
      messageId: messageId
    }
    API.delMsg(postBody).then(res => {
      this.setData({
        showPlace: false
      })
      this.onShow();
      util.toast('删除成功');
    }, err => {
      util.toast(err || '删除失败');
    })
  },

  loadData: function (options) {
    API.getMsgs(this.data.pageNo, this.data.pageSize).then(data => {
      var arr = data.messageList || [];
      if (!options) {
        arr = this.data.dataSource.concat(data.messageList || []);
      }
      let hasMore = arr.length < data.totalCount;
      this.setData({
        dataSource: arr,
        total: data.totalCount,
        hasRequest: true,
        hasMore: hasMore,
        hasLogin: true
      });
      this.getUnreadMsg();
      wx.hideLoading();
      wx.stopPullDownRefresh();
    }, err => {
      if (err == 999) {
        this.setData({
          hasLogin: false
        })
      }
      wx.hideLoading();
      wx.stopPullDownRefresh();
    });
  },

  getUnreadMsg: function () {
    API.getUnReadMsg().then(res => {
      this.setData({
        unReadNum: res
      })
    })
  },


  markAsRead: function () {
    if (this.data.unReadNum != 0) {
      wx.showLoading({
        title: '正在清除未读...',
      })
      API.markAsRead().then(data => {
        this.onShow();
        util.toast('清除未读成功');
      }, err => {
        util.toast(err || '清除未读失败');
      });
      wx.hideLoading();

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.clear();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({})
    this.loadData(true);
    wx.hideLoading();

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 重新加载
    wx.showLoading({})
    this.clear();
    this.loadData(true);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 增量加载
    if (this.data.hasMore) {
      this.setData({
        pageNo: ++this.data.pageNo
      });
      this.loadData(false);
    }
  }
})